/**
 * Date: 2019/2/20
 * Time: 18:33
 * User: Driss.
 */
public class CycledList<E> {

    //头结点
    Node<E> head;

    //尾结点
    Node<E> tail;

    int size;

    //构造函数
    public CycledList(){
        head = tail = null;
        size = 0;
    }

    //在链表头部增加结点
    public void add(E e){
        Node<E> node = new Node<E>(e);

        //如果链表为空，增加链表的第一个结点，则head=tail=node，且next指向自身
        if(head == null){
            head = tail = node;
            head.next = tail.next = node;
            size++;
        }else{

            node.next = head.next;
            head.next = node;
            if(head == tail)
                tail = node;
            size++;
        }
    }


    public int size(){
        return size;
    }





    //节点
    private static class Node<E>{
        E item;
        Node<E> next;

        Node(Node<E> node,E item){
            this.item = item;
            this.next = node;
        }

        Node(E item){
            this.item = item;
        }
    }
}
